import { createRouter, createWebHashHistory } from 'vue-router'
import { systemManage } from './system-manage/index'
import { ArticleManage } from './article-manage'
import { camelCase, upperFirst } from 'lodash'
/**
 * 获取所有路由元信息
 */
const routerMetas = import.meta.glob('../pages/**/meta.ts', {
	eager: true,
	import: 'default'
})
/**
 * 获取所有的路由信息
 */
const routerComponents = import.meta.glob('../pages/**/*.vue')
const routeDetails = Object.entries(routerComponents).map(([path, component]) => {
	const copyPath = path
	const metaPath = copyPath.replace('index.vue', 'meta.ts') ?? '/'
	const name = camelCase(copyPath.split('/').filter(Boolean)?.[2]) ?? 'index'
	const meta = routerMetas[metaPath] ?? {}
	return {
		// path: `/${name?.toLocaleLowerCase()}`,
		path: `/${name}`,
		name: upperFirst(name),
		meta,
		component
	}
})
const routes: Array<any> = [
	{
		path: '/',
		redirect: '/login'
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import('../view/login/index.vue')
	},
	{
		path: '/stage',
		name: 'Stage',
		component: () => import('@/view/stage/index.vue')
	},
	{
		path: '/category',
		name: 'Category',
		component: () => import('@/view/stage/category/index.vue')
	},
	{
		path: '/article',
		name: 'Article',
		component: () => import('@/view/article/index.vue')
	},
	{
		path: '/articleItem',
		name: 'ArticleItem',
		component: () => import('@/view/article-manage/article-list/articl-item/index.vue')
	},
	{
		path: '/home',
		name: 'Home',
		component: () => import('../view/homePage'),
		redirect: '/workSpace',
		children: [
			{
				path: '/workSpace',
				name: 'WorkSpace',
				meta: {
					title: '工作台'
				},
				component: () => import('@/view/work-space/index.vue')
			},
			...systemManage,
			...ArticleManage,
			...routeDetails
		]
	},
	{
		// path: '/:catchAll(.*)',
		path: '/:pathMatch(.*)*',
		name: 'NotFound',
		component: () => import('@/components/not-found')
	}
]
console.log(routes, 'luyou')
const router = createRouter({
	history: createWebHashHistory(),
	routes
})
router.beforeEach((to, from, next) => {
	if (!localStorage.getItem('token') && to.name != 'Login') {
		next({ path: '/login' })
		return
	}
	next()
})
export default router
